import { DataSource } from 'typeorm';


export const AppDataSource = new DataSource({
  type: 'postgres',  // 数据库类型
  host: process.env.DB_HOST || 'localhost',
  port: parseInt(process.env.DB_PORT || '5432'),
  username: process.env.DB_USER || 'eco_heaven_member1',
  password: process.env.DB_PASSWORD || 'ecoheaven',
  database: process.env.DB_NAME || 'EcoHeaven',
  entities: [`${__dirname}/../entity/**/*.ts`],  // 指定实体类文件位置
  synchronize: true,  // 自动创建和更新表结构
  logging: false,     // 日志开关，可以根据需要调整
  migrationsTableName: 'migrations', // 指定存储迁移记录的表名
  migrations: [`${__dirname}/../src/migration/*.{ts,js}`], // 指定迁移文件位置
});

export const connectDatabase = async () => {
  try {
    await AppDataSource.initialize();
    console.log('数据库连接成功');
  } catch (error) {
    console.error('数据库连接失败:', error);
    throw error;
  }
};

